package com.xumz.service.crawler;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xumz.dao.StudentDao;
import com.xumz.model.Record;
import com.xumz.model.Student;
import com.xumz.service.RecordService;
@Service
public class CrawlerService {
	@Autowired 
	private StudentDao studentDao;
	@Autowired
	RecordService recordService;
	
	
	public int collectOneStudentBoke(String stuId){	
		
		Student student= studentDao.selectOne(stuId);
		String connectUrl=student.getBlogAddress();
		Boke boke=new Boke(student.getBlogAddress()+"p/");
		student.setStuFans(Integer.parseInt(boke.bokeSuibiNum()));
		student.setStuFocus(Integer.parseInt(boke.bokepinglunNum()));
		studentDao.studentUpdateNum(student);
		
		
		List<Record> reList=recordService.selectRecordByStudentId(student.getStuId());
		List<Record> inList=recordService.getCrawler(connectUrl,stuId);

		//第一次添加该同学记录
		if (reList.isEmpty()) {
			recordService.insertList(inList);
			return 0;
		}
		//else
		List<String> tempList=new ArrayList<String>();
		for (int i = 0; i < inList.size(); i++) {
			for (int j = 0; j < reList.size(); j++) {
				if (inList.get(i).getBlogUrl().equals(reList.get(j).getBlogUrl())) {	
					tempList.add(reList.get(j).getBlogId());
				}
			}
		}
		if (tempList.isEmpty()) {
			recordService.insertList(inList);
			return 0;
		}
//		System.out.println("------------");
//		System.out.println(tempList);
		for (int i = 0; i < tempList.size(); i++) {
			recordService.deleteOne(tempList.get(i));
		}
		
		recordService.insertList(inList);
		return 0;
	}
}
